import constant from '@/constant/constant.js' // 常量
import {
	login,
	userInfo
} from '@/common/api/login.js'
import {
	systemDict,
} from '@/common/api/system.js'
export const state = {
	app_user_info: {}, // 用户信息
	dict_data:{}
};
export const mutations = {
	//储存用户信息
	setUserInfo(state, data) {
		if (data) {
			state.app_user_info = data;
			uni.setStorageSync('app_user_info', state.app_user_info);
		}
	},
	// 退出APP
	loginOut(state) {
		state.app_user_info = {};
		uni.clearStorage();
	},
	setDictData(){
		if (data) {
			state.dict_data = data;
			// uni.setStorageSync('dict_data', state.dict_data);
		}
	}
};
export const getters = {
	
}
export const actions = {
	// 登录
	Login({
		commit,
		state
	},params) {
		return new Promise((resolve, reject) => {
			let query=params.tel+'/'+params.code+'/'+params.userType
			login(query).then(res => {
				uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, res.token)
				commit('setUserInfo', res.data)
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},
	UserInfo({
		commit
	},params) {
		return new Promise((resolve, reject) => {
			userInfo(params).then(res => {
				commit('setUserInfo', res.data);
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},
	// 字典
	SystemDict({
		commit,
		state
	},params) {
		return new Promise((resolve, reject) => {
			systemDict({
				type:params
			}).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
};